package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.HistoryOfferEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.self.carMaster.HistoricalQuoteActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class HistoricalQuoteModel extends AndroidViewModel {
    private int page = 1;
    public MutableLiveData<List<HistoryOfferEntity>> refreshLiveData = new MutableLiveData<>();
    public MutableLiveData<List<HistoryOfferEntity>> loadMoreLiveData = new MutableLiveData<>();

    public HistoricalQuoteModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取历史报价数据
     *
     * @param activity
     * @param sourceNo
     * @param isRefresh
     */
    public void getHistoryData(HistoricalQuoteActivity activity, String sourceNo, boolean isRefresh) {
        page = isRefresh ? 1 : page + 1;
        DialogHelper.showProgressDialog(activity, "加载中");
        int type = UserOperating.getInstance().getUserIdentity();
        String companyNo = "", teamNo = "";
        if (type == 1) {//企业
            companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        } else if (type == 2) {//团队
            teamNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        }
        NewAPIManager.getInstance().getLogisticsAPI().getMyOfferHistoryList(sourceNo, 10, page, companyNo, teamNo).enqueue(new ICallBack<BaseEntity<ListResponse<List<HistoryOfferEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<HistoryOfferEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getState().equals(APIManager.RESPONSE_OK) && data.getData() != null) {
                    if (isRefresh) {
                        refreshLiveData.postValue(data.getData().getList());
                    } else {
                        loadMoreLiveData.postValue(data.getData().getList());
                    }
                }
            }

            @Override
            public void onFail() {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
